package com.shujia.spark.core

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Demo19SparkPi {
  def main(args: Array[String]): Unit = {
    val k: Int = 10000000

    val conf = new SparkConf()
    conf.setMaster("local[4]") // 可以指定线程数，最终决定了同一时刻可以运行多少个Task
    conf.setAppName(this.getClass.getSimpleName.replace("$", ""))
    val sc = new SparkContext(conf)

    val rdd: RDD[Int] = sc.parallelize(0 to k, 16) // 可以手动指定RDD的分区数，最终会影响Task的数量

    val pi: Double = rdd
      .map(k => {
        (4.0 / (8 * k + 1) - 2.0 / (8 * k + 4) - 1.0 / (8 * k + 5) - 1.0 / (8 * k + 6)) * 1.0 / Math.pow(16, k)
      }).sum()

    println(pi)

    //    while (true) {
    //
    //    }


  }

}
